package week04.homework01MazeGame.model.role;

import week04.homework01MazeGame.map.Maze;
import week04.homework01MazeGame.model.item.BaseItem;
import week04.homework01MazeGame.model.item.Equipment;
import week04.homework01MazeGame.model.spell.BaseSpell;
import week04.homework01MazeGame.model.spell.FireballSpell;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author li
 */
public class Player extends BaseRole {

    /**
     * 不同属性的成长值
     */
    private static final int HEALTH_GROWTH = 10;

    private static final int DEFENSE_GROWTH = 5;

    private static final int ATTACK_GROWTH = 3;

    private static final int EXP_GROWTH = 10;

    /**
     * 玩家的坐标
     */
    private int x;

    private int y;

    /**
     * 玩家的经验值和等级
     */
    private int exp = 0;

    private int level = 1;

    /**
     * 玩家所在的迷宫
     */
    private final Maze maze;

    /**
     * 玩家的背包
     */
    private final List<BaseItem> inventory;

    private final int maxCapacity = 20;

    /**
     * 玩家法术相关属性
     */
    private int mana = 50;

    private final List<BaseSpell> spells = new ArrayList<>();
    private static final Scanner SCANNER = new Scanner(System.in);

    public Player(int health, int defense, int attack, int x, int y, Maze maze) {
        super(health, defense, attack);
        this.x = x;
        this.y = y;
        this.maze = maze;
        inventory = new ArrayList<>(maxCapacity);
        spells.add(new FireballSpell());
    }

    public int getLevel() {
        return level;
    }

    public int getExp() {
        return exp;
    }

    public void addExp(int exp) {
        this.exp += exp;
        if (this.exp >= (level + 1) * EXP_GROWTH) {
            this.exp -= (level + 1) * EXP_GROWTH;
            level++;
            addHealth(HEALTH_GROWTH);
            addDefense(DEFENSE_GROWTH);
            addAttack(ATTACK_GROWTH);
            System.out.println("恭喜你升级了！当前等级：" + level);
            showMessage();
        }
    }

    public int getNextLevelExp() {
        return (level + 1) * EXP_GROWTH;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public void moveUp() {
        if (getX() == 0 || maze.getCell(getX() - 1, getY()).isObstacle()) {
            System.out.println("不能再往上走了！");
        } else {
            x--;
        }
    }

    public void moveDown() {
        if (getX() == maze.getRow() - 1 || maze.getCell(getX() + 1, getY()).isObstacle()) {
            System.out.println("不能再往下走了！");
        } else {
            x++;
        }
    }

    public void moveLeft() {
        if (getY() == 0 || maze.getCell(getX(), getY() - 1).isObstacle()) {
            System.out.println("不能再往左走了！");
        } else {
            y--;
        }
    }

    public void moveRight() {
        if (getY() == maze.getCol() - 1 || maze.getCell(getX(), getY() + 1).isObstacle()) {
            System.out.println("不能再往右走了！");
        } else {
            y++;
        }
    }

    @Override
    public void showMessage() {
        System.out.println("**** 玩家的状态： ****");
        System.out.println("生命值：" + getHealth());
        System.out.println("魔力值：" + getMana());
        System.out.println("防御力：" + getDefense());
        System.out.println("攻击力：" + getAttack());
        System.out.println("等级：" + getLevel());
        System.out.println("经验值：" + getExp());
        System.out.println("下一级所需经验值：" + getNextLevelExp());
        System.out.println("当前位置：(" + (getX() + 1) + ", " + (getY() + 1) + ")");
        System.out.println("********************");
    }

    public void magicAttack(Monster monster) {
        showSpells();
        System.out.println("请选择要施放的法术：");
        int index = SCANNER.nextInt();
        if (index >= 0 && index < spells.size()) {
            spells.get(index).cast(this, monster);
        } else {
            System.out.println("无效的法术索引");
        }
    }

    public void addItem(BaseItem item) {
        if (inventory.size() < maxCapacity) {
            int insertIndex = 0;
            for (; insertIndex < inventory.size(); insertIndex++) {
                if (!(inventory.get(insertIndex) instanceof Equipment)) {
                    break;
                }
            }
            if (!(item instanceof Equipment)) {
                inventory.add(item);
            } else {
                inventory.add(insertIndex, item);
            }
            System.out.println("获得了 " + item.getName());
        } else {
            System.out.println("背包已满，无法获得 " + item.getName());
        }
    }

    public  void showInventory() {
        System.out.println("===== 背包内容 =====");
        if (inventory.isEmpty()) {
            System.out.println("背包为空");
        } else {
            inventory.forEach(item ->
                System.out.println(item.getName() + " - " + item.getDescription()));
        }
        System.out.println("===================");
    }

    public void showSpells() {
        System.out.println("===== 法术列表 =====");
        if (spells.isEmpty()) {
            System.out.println("法术列表为空");
        } else {
            spells.forEach(spell ->
                System.out.println(spell.getSpellName() + " - " + spell.getDescription()));
        }
        System.out.println("===================");
    }

    public void useItem(int index) {
        if (index >= 0 && index < inventory.size()) {
            BaseItem item = inventory.get(index);
            item.use(this);
            inventory.remove(index);
            System.out.println("使用了 " + item.getName());
        } else {
            System.out.println("无效的物品索引");
        }
    }

    public int getMana() {
        return mana;
    }

    public void addMana(int mana) {
        this.mana += mana;
    }

    public void reduceMana(int mana) {
        this.mana -= mana;
    }

    public void addSpell(BaseSpell spell) {
        spells.add(spell);
    }
}
